﻿using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.zmc.Warehou;
using B.S.Sterilizationcenter.Read.API.Dto;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.zmc.Warehou
{
    public class CleaningorderWareStuffCommendHandlers : IRequestHandler<CleaningorderWareStuffCommends, ApiResult<List<CleaningorderWareStuffDto>>>
    {

        private readonly ISqlSugarClient db;

        public CleaningorderWareStuffCommendHandlers(ISqlSugarClient db)
        {
            this.db = db;
        }


        /// <summary>
        /// 获取清洗入库物料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<CleaningorderWareStuffDto>>> Handle(CleaningorderWareStuffCommends request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<CleaningorderWareStuffDto>> result=new ApiResult<List<CleaningorderWareStuffDto>>();
                result.Code = ApiEnums.查询成功;
                result.Msg = "查询成功";
                var list = db.Queryable<Cleaningorder, Warehous, Stuff>((o, i, c) => new JoinQueryInfos(
    JoinType.Left, o.WarehousId == i.WarehousId, //左连接 左链接 左联 
    JoinType.Left, o.StuffId == c.StuffId
))
.Select((o, i, c) => new CleaningorderWareStuffDto { 

 CleaningOrderId = o.CleaningOrderId,
    WarehousId=o.WarehousId,
    StuffId=o.StuffId,
    DeviceType=o.DeviceType,
    ProgramType = o.ProgramType,
    Duration = o.Duration,
    Result = o.Result,
    Temperature = o.Temperature,
    CreateTime=o.CreateTime,
    Status =i.Status,
    Stuffbarcode=c.Stuffbarcode,
    StuffName=c.StuffName,
    Incomquans=c.Incomquans,
})
.ToList();

                result.Data = list;
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
